package cn.com.zero.manager.auth.controller;

import cn.com.zero.api.log.annotation.Log;
import cn.com.zero.base.vo.ResponseVO;
import cn.com.zero.manager.auth.constant.LogType;
import cn.com.zero.manager.auth.constant.ValidationRule;
import cn.com.zero.manager.auth.service.IPersonalService;
import cn.com.zero.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Objects;

/**
 * @author guoxin
 * @version 1.0.0
 * @Description
 * @date 2023/4/10 9:24
 */
@Api(tags = "个人信息管理接口")
@RestController
@Validated
@RequestMapping("/manager/personal")
public class PersonalController extends BaseController {

    /**
     * 个人信息管理service
     */
    private IPersonalService personalService;

    @ApiOperation(value = "修改个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName",
                    value = "名称,长度在 " + ValidationRule.ADMIN_NAME_MIN + "~" + ValidationRule.ADMIN_NAME_MAX + " 之间"),
            @ApiImplicitParam(paramType = "query", name = "mobile",
                    value = "手机号,长度为 " + ValidationRule.MOBILE_PHONE_LENGTH)
    })
    @Log(content = "修改个人信息", logType = LogType.LOG_MANAGE)
    @PutMapping("/updateInfo")
    public ResponseVO updateInfo(
            @Size(min = ValidationRule.ADMIN_NAME_MIN, max = ValidationRule.ADMIN_NAME_MAX, message = "{range}") @RequestParam(value = "userName", required = false) String userName,
            @Pattern(regexp = ValidationRule.MOBILE_EX) @RequestParam(value = "mobile", required = false) String mobile) {
        logger.debug("修改个人信息");
        ResponseVO responseVO = personalService.updateInfo(userName,mobile);
        if(Objects.isNull(responseVO)){
            ResponseVO vo = new ResponseVO();
            vo.setMessage("修改失败");
            return vo;
        }
        return responseVO;
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "oldPassword", required = true, value = "原密码，不允许为空，需要在客户端对原密码进行 MD5 加密后传到服务端"),
            @ApiImplicitParam(paramType = "query", name = "newPassword", required = true, value = "新密码，不允许为空，客户端限制 6~18 位长度，字母+数字，需要在客户端对新密码进行 MD5 加密后传到服务端")
    })
    @Log(content = "修改密码", logType = LogType.LOG_MANAGE, ignoreFields = {"oldPassword", "newPassword"})
    @PutMapping("/updatePassword")
    public ResponseVO updatePassword(@RequestParam("oldPassword") String oldPassword
            , @Pattern(regexp = ValidationRule.PASSWORD_EX) @RequestParam("newPassword") String newPassword) {
        logger.debug("修改密码");
        ResponseVO responseVO = personalService.updatePassword(oldPassword,newPassword);
        if(Objects.isNull(responseVO)){
            ResponseVO vo = new ResponseVO();
            vo.setMessage("修改失败");
            return vo;
        }
        return responseVO;
    }

    @Autowired
    public void setPersonalService(IPersonalService personalService) {
        this.personalService = personalService;
    }
}
